package com.eqdd.yiqidian.utils;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by jhj on 18-4-20.
 */

public class TimeUtil {

    /**
     * @param time yyyy-MM-dd T HH:mm:ss.00
     * @return yyyy-MM-dd HH:mm
     */
    public static String formatWebTime(String time) {
        if (time == null) {
            return "";
        }
        return time
                .substring(0, time.lastIndexOf(":"))
                .replace("T", " ");
    }

    /**
     * 去除秒
     *
     * @param time HH:mm:ss
     * @return HH:mm
     */
    public static String formatWebTime1(String time) {
        if (time == null) {
            return "";
        }
        return time.substring(0, time.lastIndexOf(":"));
    }


    /**
     * 天数计算
     *
     * @param start
     * @param end
     * @return
     */
    public static String calculateDate(Date start, Date end) {
        long time = end.getTime() - start.getTime();
        double day = TimeUnit.MILLISECONDS.toDays(time);
        long hour = TimeUnit.MILLISECONDS.toHours(time);
        if (hour % 24 != 0) {//当时２４小时倍数时，直接去天数
            if (start.getHours() > end.getHours()) {//跨天请假，并且结束时间的整点数小于请假时请假时的整点数
                int time1 = 18 - start.getHours();
                int time2 = end.getHours() - 8;
                if (time1 + time2 < 4) {
                    day += 0.5;
                } else {
                    day += 1;
                }

            } else {
                int startHour = start.getHours();
                int endHour = end.getHours();
                if (start.getHours() < 8) {
                    startHour = 8;
                }
                if (end.getHours() > 16) {
                    endHour = 18;
                }
                int mHour = endHour - startHour;
                if (mHour < 3) {
                    day += 0.5;
                } else {
                    day += 1;
                }
            }

        }
        return String.valueOf(day);
    }

}
